package com.tencent.qcloud.tim.uikit.component.goods

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.tencent.qcloud.tim.uikit.R
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack
import com.tencent.qcloud.tim.uikit.databinding.ActivitySendGoodsListBinding

class SendGoodsListActivity :
    BaseActivity<SendGoodsListViewModel, ActivitySendGoodsListBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_send_goods_list

    private var storeId: String? = ""
    private var content = ""
    private var sort = "featured"
    private var pageNumber = 1
    private val mAdapter by lazy { SendGoodsListAdapter() }

    override fun onGetBundle(bundle: Bundle?) {
        storeId = bundle?.getString("storeId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.choose_goods))
        mBinding.viewModel = viewModel
        mBinding.lvGoofs.apply {
            adapter = mAdapter
            layoutManager = GridLayoutManager(mContext, 2)
        }
    }

    override fun observeData() {
        mBinding.statusView.showLoading()
        getData()
        viewModel.goodlist.observe(this, Observer {
            when (pageNumber) {
                1 -> mAdapter.setNewData(it)
                else -> mAdapter.addData(it)
            }
            mBinding.refresh.onLoad(it.size)
            when (mAdapter.mData.size) {
                0 -> {
                    mBinding.statusView.showEmpty()
                }
                else -> {
                    mBinding.statusView.showContent()
                }
            }
        })
    }

    fun getData() {
        viewModel.getGoodsList(
            content,
            storeId?.toInt(),
            null,
            sort,
            pageNumber
        )
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                mBinding.statusView.showError()
            }
        }
    }

    override fun eventHandling() {
        mBinding.refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                getData()
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                getData()
            }
        })
        mAdapter.onClickSendGoodsListener = {
            if (mCallBack != null) {
                mCallBack?.onSuccess(it)
                finish()
            }
        }
        mBinding.statusView.setOnRetryClickListener(View.OnClickListener {
            pageNumber = 1
            getData()
        })
        mBinding.etSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                content = mBinding.etSearch.text.toString()
                pageNumber = 1
                getData()
                true
            }
            false
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        mCallBack = null
    }

    companion object {
        var mCallBack: IUIKitCallBack? = null
        fun openActivity(mContext: Context, storeId: String) {
            val bundle = Bundle().apply {
                putString("storeId", storeId)
            }
            val intent = Intent(mContext, SendGoodsListActivity::class.java).apply {
                putExtras(bundle)
            }
            mContext.startActivity(intent)
        }
    }
}